package com.ccnf.controller;

import com.ccnf.core.bean.Response;
import com.ccnf.core.bean.ResponseWithData;
import com.ccnf.model.ao.PassToken;
import com.ccnf.model.ao.SmsAO;
import com.ccnf.model.entity.Invitation;
import com.ccnf.model.entity.InvitationRegister;
import com.ccnf.model.enums.AuthStatusEnum;
import com.ccnf.service.InvitationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/invitation")
@Api(value = "InviController", description = "邀请函相关API")
public class InviController extends BaseController {
    @Autowired
    private InvitationService invitationService;

    @ApiOperation(value = "录入邀请函数据")
    @PostMapping
    public ResponseWithData addInvitation(@RequestBody Invitation invitation) {
        return ResponseWithData.success(invitationService.saveInvitation(invitation, getTokenParams()));
    }

    @ApiOperation("批量导入邀请函数据（json）")
    @PostMapping("batch/insert")
    @PassToken
    public Response batchImportInvi(@RequestBody List<Invitation> invitationList, Long expoId) {
        invitationService.batchImportInvi(expoId, invitationList);
        return Response.success();
    }

    @ApiOperation("搜索邀请函列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchContent", value = "🔍搜索内容", paramType = "query"),
            @ApiImplicitParam(name = "authStatus", value = "授权类型", paramType = "query"),
            @ApiImplicitParam(name = "demo", value = "模板类型", paramType = "query")
    })
    @GetMapping("search")
    public ResponseWithData invitationList(@RequestParam(required = false) String searchContent,
                                           @RequestParam(required = false) AuthStatusEnum authStatus,
                                           @RequestParam(required = false) Integer demo) {
        return ResponseWithData.success(invitationService.listInvitationBySearch(getTokenParams(),
                searchContent, authStatus, demo));
    }

    @ApiOperation("获取邀请函详细内容")
    @GetMapping("detail/{inviId}")
    @PassToken
    public ResponseWithData inviDetailInfo(@PathVariable Long inviId) {
        return ResponseWithData.success(invitationService.findInvitationDetailInfo(inviId, getTokenParams()));
    }

    @ApiOperation("获取邀请函基本内容")
    @GetMapping("basic/{inviId}")
    @PassToken
    public ResponseWithData inviBasicInfo(@PathVariable Long inviId) {
        return ResponseWithData.success(invitationService.finInvitationBasicInfo(inviId));
    }

    @ApiOperation("获取邀请函分享ID")
    @GetMapping("share/{inviId}")
    public ResponseWithData inviUserShare(@PathVariable Long inviId) {
        return ResponseWithData.success(invitationService.findInviUser(inviId, getTokenParams()));
    }

    @ApiOperation("修改邀请函内容")
    @PatchMapping("{inviId}")
    public Response updateInviInfo(@PathVariable Long inviId, @RequestBody Invitation invitation) {
        invitationService.modifyInvitation(inviId, invitation);
        return Response.success();
    }

    @ApiOperation("获取授权信息")
    @GetMapping("auth/code/{inviId}")
    public ResponseWithData getAuthCode(@PathVariable Long inviId) {
        return ResponseWithData.success(invitationService.createInviAuthCode(inviId, getTokenParams()));
    }

    @ApiOperation("获取授权邀请函列表")
    @GetMapping("auth/list")
    public ResponseWithData authInvi() {
        return ResponseWithData.success(invitationService.listAuthInvi(getTokenParams()));
    }

    @ApiOperation("获取授权的邀请函列表")
    @GetMapping("auth")
    public ResponseWithData userInvi() {
        return ResponseWithData.success(invitationService.listUserInviByUserId(getTokenParams()));
    }

    @ApiOperation("录入邀请函登记数据")
    @PassToken
    @PostMapping("register")
    public Response register(@RequestBody InvitationRegister invitationRegister) {
        invitationRegister.setInviId(invitationRegister.getId());
        invitationRegister.setId(null);
        invitationService.registerInvi(invitationRegister);
        return Response.success();
    }

    @ApiOperation("邀请函报表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inviId", value = "邀请函ID", paramType = "path"),
            @ApiImplicitParam(name = "reportType", value = "报表类型（0：浏览，1：登记，2：分享，3：收藏）", paramType = "query"),
            @ApiImplicitParam(name = "dayNum", value = "报表天数", paramType = "query")
    })
    @GetMapping("report/{inviId}")
    public ResponseWithData invitationReport(@PathVariable Long inviId, Integer reportType, Integer dayNum) {
        return ResponseWithData.success(invitationService.findInvitationReport(inviId, dayNum, reportType));
    }

    @ApiOperation("邀请函统计报表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordType", value = "记录状态（0：总，1：日）", paramType = "query")
    })
    @GetMapping("record/count")
    public ResponseWithData inviReport(Integer recordType) {
        return ResponseWithData.success(invitationService.findInviCountRecord(recordType, getTokenParams()));
    }

    @ApiOperation("邀请函报表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportType", value = "报表类型（0：浏览，1：登记，2：收藏，3：转发点击）", paramType = "query"),
            @ApiImplicitParam(name = "dayNum", value = "报表天数", paramType = "query")
    })
    @GetMapping("report/count")
    public ResponseWithData invitationReport(Integer reportType, Integer dayNum) {
        return ResponseWithData.success(invitationService.findInviCountReport(dayNum, reportType, getTokenParams()));
    }

    @ApiOperation("用户[进行/取消]收藏邀请函")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inviId", value = "邀请函ID", paramType = "path"),
            @ApiImplicitParam(name = "isCollect", value = "是否收藏（0：否，1：是）", paramType = "query")
    })
    @GetMapping("collect/{inviId}")
    public Response collectInvi(@PathVariable Long inviId,
                                @RequestParam(required = false, defaultValue = "0") Integer isCollect) {
        invitationService.collectInvitation(inviId, isCollect, getTokenParams());
        return Response.success();
    }

    @ApiOperation("邀请函收藏列表")
    @GetMapping("collect/list")
    public ResponseWithData inviCollectList() {
        return ResponseWithData.success(invitationService.listInvitationCollect(getTokenParams()));
    }

    @ApiOperation("邀请函发送邀约短信")
    @PostMapping("sms/{inviId}")
    public ResponseWithData sendSms(@PathVariable Long inviId, @RequestBody List<SmsAO> sms) {
        return ResponseWithData.success(invitationService.sendInvitationSms(inviId, sms, getTokenParams()));
    }

    @ApiOperation("获取邀请函总统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inviId", value = "邀请函ID", paramType = "path"),
            @ApiImplicitParam(name = "recordType", value = "记录状态（0：总，1：日）", paramType = "query")
    })
    @GetMapping("record/{inviId}")
    public ResponseWithData inviRecord(@PathVariable Long inviId, Integer recordType) {
        return ResponseWithData.success(invitationService.findInvitationRecord(inviId, recordType));
    }

    @ApiOperation("获取邀请函排名列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rankType", value = "排名类型（0：浏览，1：登记，2：收藏，3：转发点击）", paramType = "query")
    })
    @GetMapping("record/rank")
    public ResponseWithData inviRecord(Integer rankType) {
        return ResponseWithData.success(invitationService.listInvitationRecordRank(rankType, getTokenParams()));
    }

    @ApiOperation("邀请函报表邮件导出")
    @GetMapping("report/mail/export")
    public Response inviReportExport(String email, Long inviId) {
        invitationService.handleInviDataExport(getTokenParams(), email, inviId);
        return Response.success();
    }
}